#include "pkmlparser.h"

PKmlParser::PKmlParser()
{
}

PKmlParser::PKmlParserErr  PKmlParser::parse(const QString &file_name, bool *success)
{
    *success = parseXmlFile(file_name,"kml");
    if (!*success) return ErrXmlFile;

    QString content_string;

    ///////////////////////////////////////////////////
    // Traduzione di                      <coordinates>
    ///////////////////////////////////////////////////
    content_string = parseElementChildren(
                "Document,Placemark,Point,coordinates",
                success);
    if (!*success) return ErrXmlChildrenL;
    m_contentTag_coordinates = content_string;

    ///////////////////////////////////////////////////
    // Traduzione di                             <name>
    ///////////////////////////////////////////////////
    content_string = parseElementChildren(
                "Document,Placemark,name",
                success);
    if (!*success) return ErrXmlChildrenL;
    m_contentTag_name = content_string;

    ///////////////////////////////////////////////////
    // Traduzione di                      <description>
    ///////////////////////////////////////////////////
    content_string = parseElementChildren(
                "Document,Placemark,description",
                success);
    if (!*success) return ErrXmlChildrenL;
    m_contentTag_description = content_string;

    qDebug()<<"content_string"<<content_string;

    return ErrOk;
}

PKmlParser::PKmlParserErr PKmlParser::extractObj(EPoint &epoint)
{
    QStringList l = m_contentTag_coordinates.split(",");
    epoint.set_rif(EPoint::RIF_WGS84);

    bool ok;

    // Traduco LON
    ok = l.at(0).toDouble();
    if (!ok)
    {
        return ErrKmlCoordinatesLat;
    }
    epoint.set_lon(l.at(0).toDouble());

    // Traduco LAT
    ok = l.at(1).toDouble();
    if (!ok)
    {
        return ErrKmlCoordinatesLon;
    }
    epoint.set_lat(l.at(1).toDouble());

    qDebug()<<QString("Lat = %1 - Lon = %2").arg(l.at(0)).arg(l.at(1));
    // TODO: aggiungere elevazione e point.
    // point.set_ele(l.at(2).toDouble());
    return ErrOk;
}

PKmlParser::PKmlParserErr PKmlParser::extractObj(QString &name, QString &descr)
{
    name = m_contentTag_name;
    descr = m_contentTag_description;
    return ErrOk;
}
